/*
 * File:    VariableStoreNode.java
 * Created: 19-Feb-2009
 * Version: $Id$
 *
 * COPYRIGHT (C) 2009, Bitgate Mobile, LLC.  All Rights Reserved.
 *
 * software@bitgatemobile.com
 */

package com.webplasm.store;

import static com.bitgate.util.debug.Debug.debug;

import org.w3c.dom.Node;

import com.webplasm.render.engine.DocumentParser;
import com.webplasm.store.VariableContainer.VariableType;

public class VariableStoreNode extends AbstractVariableStore {
	private Node value;
	
	public VariableType getType() {
		return VariableType.NODE;
	}
	
	@Override
	public boolean isStandard() {
		return true;
	}
	
	/**
	 * Set value is sent a <code>String</code> object, which must be walked and parsed using SAX.
	 * 
	 * @param value The value to parse.
	 */
	@Override
	public void setValue(Object value) {
		DocumentParser dParser = null;
		
		try {
			debug("Data: " + new String((String) value));
			dParser = new DocumentParser(((String) value).getBytes());
		} catch(Exception ex) {
			debug("Unable to parse document.");
			this.value = null;
			
			throw new RuntimeException("Unable to parse document", ex);
		}
		
		this.value = dParser.getRootNode();
		
		debug("Document parsed correctly.");
	}
	
	@Override
	public Object getObject() {
		return value;
	}
}